package reesoft.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 从文件加载配置
 *
 * Created by reesoft on 2017-07-14
 */
public class PropertiesUtil
{
    private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class);

    /**
     * 从文件加载配置
     *
     * @param filePath 文件路径
     * @return Properties 对象
     */
    public static Properties load(final String filePath)
    {
        if (LOG.isDebugEnabled())
        {
            LOG.debug("load properties filePath:" + filePath);
        }

        Properties properties = new Properties();
        InputStream inputStream = PropertiesUtil.class.getResourceAsStream(filePath);

        try
        {
            if (inputStream != null)
            {
                properties.load(inputStream);
            }
        }
        catch (IOException e)
        {
            LOG.error("load properties file " + filePath + " failed");
        }
        finally
        {
            try
            {
                if (inputStream != null)
                {
                    inputStream.close();
                }
            }
            catch (Exception e)
            {
                LOG.warn("close input stream failed");
            }
        }

        return properties;
    }
}
